package com.xbongbong.paas.help;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.model.CustomButtonActionModel;
import com.xbongbong.paas.model.CustomButtonModel;
import com.xbongbong.paas.model.LowCodeActionSettingModel;
import com.xbongbong.paas.model.LowCodeJsFileModel;
import com.xbongbong.paas.model.PaasFormServiceModel;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.domain.entity.CustomButtonActionEntity;
import com.xbongbong.pro.domain.entity.CustomButtonEntity;
import com.xbongbong.pro.domain.entity.LowCodeActionSettingEntity;
import com.xbongbong.pro.domain.entity.LowCodeJsFileEntity;
import com.xbongbong.pro.domain.entity.PaasFormServiceEntity;
import com.xbongbong.pro.lowcodejsfile.pojo.JsFilePojo;
import com.xbongbong.pro.service.aliyun.AliyunOssService;
import com.xbongbong.saas.constant.StringConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 *
 * 低代码相关帮助类
 * @author 李少杰
 * @version v1.0.0
 * @date 2022/3/1 09:17
 */
@Component
public class LowCodeHelp {
    private static final Logger LOG = LoggerFactory.getLogger(LowCodeHelp.class);
    @Resource
    private LowCodeJsFileModel lowCodeJsFileModel;
    @Resource
    private CustomButtonModel customButtonModel;
    @Resource
    private CustomButtonActionModel customButtonActionModel;
    @Resource
    private LowCodeActionSettingModel lowCodeActionSettingModel;
    @Resource
    private PaasFormServiceModel paasFormServiceModel;
    @Resource
    private ProBaseConfig proBaseConfig;
    @Resource
    private AliyunOssService aliyunOssService;


    /**
     * 删除低代码-非工单业务调用
     * @param corpId
     * @param formIdList
     */
    public void deleteLowCode(String corpId, Collection<Long> formIdList) {
        deleteLowCode(corpId, formIdList, null);
    }

    /**
     * 删除低代码
     * @param corpId
     * @param formIdList
     */
    public void deleteLowCode(String corpId, Collection<Long> formIdList, Integer businessType) {
        HashMap<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.CORPID, corpId);
        param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
        param.put("formIdIn", formIdList);
        if (Objects.nonNull(businessType)) {
            param.put(StringConstant.BUSINESS_TYPE, businessType);
        }

        List<CustomButtonEntity> customButtonEntityList = customButtonModel.findEntitys(param);
        if (CollectionsUtil.isNotEmpty(customButtonEntityList)) {
            List<Long> idList = customButtonEntityList.stream().map(CustomButtonEntity::getId).collect(Collectors.toList());
            customButtonModel.deleteBatch(idList, corpId);
        }

        List<CustomButtonActionEntity> customButtonActionEntityList = customButtonActionModel.findEntitys(param);
        if (CollectionsUtil.isNotEmpty(customButtonActionEntityList)) {
            List<Long> idList = customButtonActionEntityList.stream().map(CustomButtonActionEntity::getId).collect(Collectors.toList());
            customButtonActionModel.deleteBatch(idList, corpId);
        }

        List<LowCodeActionSettingEntity> lowCodeActionSettingEntityList = lowCodeActionSettingModel.findEntitys(param);
        if (CollectionsUtil.isNotEmpty(lowCodeActionSettingEntityList)) {
            List<Long> idList = lowCodeActionSettingEntityList.stream().map(LowCodeActionSettingEntity::getId).collect(Collectors.toList());
            lowCodeActionSettingModel.deleteBatch(idList, corpId);
        }

        List<LowCodeJsFileEntity> lowCodeJsFileEntityList = lowCodeJsFileModel.findEntitys(param);
        if (CollectionsUtil.isNotEmpty(lowCodeJsFileEntityList)) {
            List<Long> idList = lowCodeJsFileEntityList.stream().map(LowCodeJsFileEntity::getId).collect(Collectors.toList());
            lowCodeJsFileModel.deleteBatch(idList, corpId);
        }

        List<PaasFormServiceEntity> paasFormServiceEntityList = paasFormServiceModel.findEntitys(param);
        if (CollectionsUtil.isNotEmpty(paasFormServiceEntityList)) {
            List<Long> idList = paasFormServiceEntityList.stream().map(PaasFormServiceEntity::getId).collect(Collectors.toList());
            paasFormServiceModel.deleteBatch(idList, corpId);
        }
    }

    /**
     * 删除低代码及配置-排除服务注册
     * @param corpId
     * @param formIdList
     * @param businessType
     */
    public void deleteLowCodeConfig(String corpId, Collection<Long> formIdList, Integer businessType) {
        HashMap<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.CORPID, corpId);
        param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
        param.put("formIdIn", formIdList);
        if (Objects.nonNull(businessType)) {
            param.put(StringConstant.BUSINESS_TYPE, businessType);
        }

        List<CustomButtonEntity> customButtonEntityList = customButtonModel.findEntitys(param);
        if (CollectionsUtil.isNotEmpty(customButtonEntityList)) {
            List<Long> idList = customButtonEntityList.stream().map(CustomButtonEntity::getId).collect(Collectors.toList());
            customButtonModel.deleteBatch(idList, corpId);
        }

        List<CustomButtonActionEntity> customButtonActionEntityList = customButtonActionModel.findEntitys(param);
        if (CollectionsUtil.isNotEmpty(customButtonActionEntityList)) {
            List<Long> idList = customButtonActionEntityList.stream().map(CustomButtonActionEntity::getId).collect(Collectors.toList());
            customButtonActionModel.deleteBatch(idList, corpId);
        }

        List<LowCodeActionSettingEntity> lowCodeActionSettingEntityList = lowCodeActionSettingModel.findEntitys(param);
        if (CollectionsUtil.isNotEmpty(lowCodeActionSettingEntityList)) {
            List<Long> idList = lowCodeActionSettingEntityList.stream().map(LowCodeActionSettingEntity::getId).collect(Collectors.toList());
            lowCodeActionSettingModel.deleteBatch(idList, corpId);
        }

        List<LowCodeJsFileEntity> lowCodeJsFileEntityList = lowCodeJsFileModel.findEntitys(param);
        if (CollectionsUtil.isNotEmpty(lowCodeJsFileEntityList)) {
            List<Long> idList = lowCodeJsFileEntityList.stream().map(LowCodeJsFileEntity::getId).collect(Collectors.toList());
            lowCodeJsFileModel.deleteBatch(idList, corpId);
        }
    }

    /**
     * 拷贝低代码文件
     * @param jsFilePojoStr
     * @param suffix
     * @return
     */
    public String copyLowCodeRepFile(String jsFilePojoStr, String suffix, String corpid, Long appId, Long formId) {
        try {
            if (Objects.nonNull(jsFilePojoStr) && StringUtil.isNotEmpty(jsFilePojoStr)) {
                JsFilePojo jsFilePojo = JSON.parseObject(jsFilePojoStr, JsFilePojo.class);
                if (Objects.isNull(jsFilePojo) || Objects.isNull(jsFilePojo.getAttachIndex())) {
                    return null;
                }
                // 获取源文件路径
                String host = "https://" + proBaseConfig.getLowCodeBucketName() + StringConstant.POINT + proBaseConfig.getLowCodeEndpointSimple();
                String attachIndex = jsFilePojo.getAttachIndex();
                String fileDir = attachIndex.replace(host, "");
                String sourcePath = fileDir.substring(1);

                // 目标文件名
                String targetFileName = formId + StringConstant.CROSS + suffix;
                String targetPath = ProBaseConfig.getProjectName() + StringConstant.SLASH + corpid + StringConstant.SLASH + appId + StringConstant.SLASH + targetFileName;

                aliyunOssService.copyLowCodeFile(sourcePath, targetPath);
                String targetIndex = host + StringConstant.SLASH + targetPath;
                jsFilePojo.setAttachIndex(targetIndex);
                jsFilePojo.setFilename(targetFileName);
                return JSON.toJSONString(jsFilePojo);
            }
        } catch (Exception e) {
            LOG.error("低代码文件拷贝失败! ", e);
        }
        return null;
    }
}
